package com.feixiang.springbootinit.strategy;
import com.feixiang.springbootinit.model.enums.SessionArchiveMsgTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
public class MessageStrategyFactory {

    private final List<MessageProcessingStrategy> strategies;
    private final DefaultMessageStrategy defaultStrategy;

    @Autowired
    public MessageStrategyFactory(List<MessageProcessingStrategy> strategies,
                                  DefaultMessageStrategy defaultStrategy) {
        this.strategies = strategies;
        this.defaultStrategy = defaultStrategy;
        log.info("已加载的策略列表: {}",
                strategies.stream()
                        .map(s -> s.getClass().getSimpleName())
                        .collect(Collectors.toList()));
    }

    public MessageProcessingStrategy getStrategy(SessionArchiveMsgTypeEnum msgType) {
        return strategies.stream()
                .filter(strategy -> strategy.supports(msgType))
                .findFirst()
                //找不到匹配的策略时，返回默认策略
                .orElse(defaultStrategy);
    }
}